import time

from auto_uitest.ranzhiUitest.baw.loginpage import LoginPage
from auto_uitest.ranzhiUitest.caw.basepage import BasePage, getDriver


class AddUser(BasePage):
    dw_houtai = "id = s-menu-superadmin"
    dw_iframe= "id = iframe-superadmin"
    dw_tianjia ="class name = shortcut.user"
    dw_username= "id = account"
    dw_realname= "id = realname"
    dw_genderm = "id = genderm"
    dw_genderf = "id = genderf"
    dw_dept = "id = dept"
    dw_role = "id = role"
    dw_pwd1 = "id = password1"
    dw_pwd2 = "id = password2"
    dw_email = "id = email"
    dw_submit = "id = submit"

    # 成功
    dw_query = "id = query"

    # 失败
    dw_accountLabel = "id = accountLabel"
    dw_realnameLabel= "id = realnameLabel"
    dw_roleLabel = "id = roleLabel"
    dw_password1Label = "id = password1Label"
    dw_emailLabel="id = emailLabel"


    def adduser(self,username,realname,gender,dept,role,pwd1,pwd2,email):
        self.click_element(self.dw_houtai)
        self.switch_to_iframe(self.dw_iframe)
        self.click_element(self.dw_tianjia)

        # 添加成员，发送数据
        self.send_information(self.dw_username,username)
        self.send_information(self.dw_realname,realname)

        if gender=="男":
            # 男
            self.click_element(self.dw_genderm)
        else:
            # 女
            self.click_element(self.dw_genderf)

        # 部门
        self.select_by_text(self.dw_dept,dept)
        # 角色
        self.select_by_text(self.dw_role,role)
        # 密码
        self.send_information(self.dw_pwd1,pwd1)
        self.send_information(self.dw_pwd2,pwd2)
        # 邮箱
        self.send_information(self.dw_email,email)

        # 点击保存
        self.click_element(self.dw_submit)

    # 检查成功
    def check_success(self):
        element = self.locate_element(self.dw_query)
        if element is not None:
            print("保存成功")

    # 检查失败
    def check_fail(self,errinfo): # errinfo 预期的失败原因
        # 预期和页面的失败原因进行对比预判
        if "用户名" in errinfo :
            # 获取用户名的页面错误信息
            info = self.locate_element(self.dw_accountLabel).text
        elif "真实姓名" in errinfo:
            info = self.locate_element(self.dw_realnameLabel).text
        elif "角色" in errinfo:
            info = self.locate_element(self.dw_roleLabel).text
        elif "密码" in errinfo:
            info = self.locate_element(self.dw_password1Label).text
        elif "邮箱" in errinfo:
            info = self.locate_element(self.dw_emailLabel).text

        return  errinfo == info







if __name__ == '__main__':
    # 获取驱动
    d = getDriver("edge")
    lp = LoginPage(d)
    # 打开页面
    lp.open("http://127.0.0.1/ranzhi/www/")
    # 登录
    lp.login("admin", "123456")

    au = AddUser(d)
    au.adduser("admin4","张三1","女","/广告部","行政","1234567","1234567","sakgj123@jgad.com")
    au.check_success()
    # print(au.check_fail("用户名已经有admin1这条记录了。"))
    time.sleep(3)
    # 退出登录
    lp.logout()
    time.sleep(3)








